/*!
 * HTML5 canvaslib
 * 
 * Copyright 2011
 * by @AbdullahDiaa
 * Released under GNU GPLv2 license .
 * Date: Mon July 25 14:16:56 2011
 * 
 */
// canvaslib main Object
var canvaslib = {};
canvaslib.draw = draw = {
    // "2d" context of the canvas
    ctx: null,
    width: 0,
    height: 0,
    // "draw.Start" by getting the main canvas with its ID and getting its width and height
    Start: function(id) {
        var canvas = document.getElementById(id);
        this.ctx = canvas.getContext("2d");
        this.width = canvas.width;
        this.height = canvas.height;
    },
    // "draw.circle"
    circle: function(x, y, r, fillcolor) {
        this.ctx.save();
        //default fill color value set to black
        if (typeof fillcolor == "undefined") {
            fillcolor = "#000";
        }
        //draw circle 360 degree arc
        this.ctx.beginPath();
        this.ctx.fillStyle = fillcolor;
        this.ctx.arc(x, y, r, Math.PI * 2, false);
        this.ctx.closePath();
        this.ctx.fill();
        this.ctx.restore();
    },
    // "draw.rectangle"
    rectangle: function(x, y, width, height, fillcolor) {
        this.ctx.save();
        //default fill color value set to black
        if (typeof fillcolor == "undefined") {
            fillcolor = "#000";
        }
        this.ctx.fillStyle = fillcolor;
        this.ctx.fillRect(x, y, width, height);
        this.ctx.restore();
    },
    // "draw.square"
    square: function(x, y, width, fillcolor) {
        this.ctx.save();
        //default fill color value set to black
        if (typeof fillcolor == "undefined") {
            fillcolor = "#000";
        }
        this.ctx.fillStyle = fillcolor;
        this.ctx.fillRect(x, y, width, width);
        this.ctx.restore();
    },
    // "draw.spiral"
    spiral: function(w, z, spirals, space, color) {
		 this.ctx.save();
		 this.ctx.translate(w, z);
		 var a = 0;
		 var b = space;
		 for (i = 0; i < spirals; i += 0.1) {
		     if ((i % 10) === 0) {
		         b += 0.5;
		     }
		     r = a + (b * i);
		     x = r * Math.cos(i);
		     y = r * Math.sin(i);
		     this.ctx.lineTo(x, y);
		 }
		//default fill color value set to black
		 if (typeof color == "undefined") {
		     color = "#000";
		 }
		 this.ctx.strokeStyle = color;
		 this.ctx.stroke();
		 this.ctx.restore();
    },
    // "draw.text"
    text: function(x, y, text, font, color) {
		//default fill color value set to black
		if (typeof color == "undefined") {
		    color = "#000";
		}
		//default font weight / size / font-family
		if (typeof font == "undefined") {
		    font = "bold 12px Arial,sans-serif";
		}
		this.ctx.save();
		this.ctx.font = font;
		this.ctx.fillStyle = color;
		this.ctx.fillText(text, x, y);
		this.ctx.restore();
    },
    // "draw.image"
    image: function(x, y, src) {
        this.ctx.save();
        var Img = new Image();
        Img.src = src;
        this.ctx.drawImage(Img, x, y);
        this.ctx.restore();
    },
	// clear the canvas 
	clear : function(){
		this.ctx.clearRect(0,0,this.width,this.height);
	},
	// get Image Data in CanvasPixelsArray
	getpixels : function(x, y, w, h){
		 // get pixels data in CanvasPixelArray from area with width w and height h from the canvas
		 var imgd = this.ctx.getImageData(x, y, w, h);
		 return imgd.data;
	},

}